<html>
<head><meta charset="utf-8"><title>DefId to Ty for a generic struct · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/DefId.20to.20Ty.20for.20a.20generic.20struct.html">DefId to Ty for a generic struct</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="221149556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/DefId%20to%20Ty%20for%20a%20generic%20struct/near/221149556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/DefId.20to.20Ty.20for.20a.20generic.20struct.html#221149556">(Dec 29 2020 at 17:12)</a>:</h4>
<p>Hi. How can I build a <code>Ty</code> for a <em>generic</em> struct, given the <code>DefId</code> of its definition and the <code>Ty</code> of the (one) type parameter?</p>



<a name="221149589"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/DefId%20to%20Ty%20for%20a%20generic%20struct/near/221149589" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/DefId.20to.20Ty.20for.20a.20generic.20struct.html#221149589">(Dec 29 2020 at 17:13)</a>:</h4>
<p>is <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html#method.type_of">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html#method.type_of</a> what you want?</p>



<a name="221149617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/DefId%20to%20Ty%20for%20a%20generic%20struct/near/221149617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/DefId.20to.20Ty.20for.20a.20generic.20struct.html#221149617">(Dec 29 2020 at 17:13)</a>:</h4>
<p>I was about to type, when the struct was non-generic I could use that :)</p>



<a name="221149630"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/DefId%20to%20Ty%20for%20a%20generic%20struct/near/221149630" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/DefId.20to.20Ty.20for.20a.20generic.20struct.html#221149630">(Dec 29 2020 at 17:13)</a>:</h4>
<p>But how to specify the parameter now that it is?</p>



<a name="221149690"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/DefId%20to%20Ty%20for%20a%20generic%20struct/near/221149690" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/DefId.20to.20Ty.20for.20a.20generic.20struct.html#221149690">(Dec 29 2020 at 17:14)</a>:</h4>
<p>you can then use <code>.subst(substs)</code> to apply your generic parameters</p>



<a name="221149725"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/DefId%20to%20Ty%20for%20a%20generic%20struct/near/221149725" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/DefId.20to.20Ty.20for.20a.20generic.20struct.html#221149725">(Dec 29 2020 at 17:14)</a>:</h4>
<p>(requires a bit more arguments <span aria-label="grinning" class="emoji emoji-1f600" role="img" title="grinning">:grinning:</span>, but generally, the <code>subst</code> method is what you want, you need to import the <code>Subst</code> trait for that to be available I think</p>



<a name="221150233"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/DefId%20to%20Ty%20for%20a%20generic%20struct/near/221150233" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/DefId.20to.20Ty.20for.20a.20generic.20struct.html#221150233">(Dec 29 2020 at 17:21)</a>:</h4>
<p>Looks like that worked, thank you both!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>